{{define "system/role/dataScope"}}
<!DOCTYPE html>
<html lang="zh">
<head>
    {{template "header" (OssUrl)}}
    <link href="{{OssUrl}}/resource/ajax/libs/jquery-ztree/3.5/css/metro/zTreeStyle.css" rel="stylesheet"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content   ibox-content">
    <form class="form-horizontal m" id="form-role-edit">
        <input id="roleId" name="roleId" type="hidden" value="{{.role.RoleId}}"/>
        <div class="form-group">
            <label class="col-sm-3 control-label ">角色名称：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="roleName" id="roleName" value="{{.role.RoleName}}"
                       readonly="true"/>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">权限字符：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="roleKey" id="roleKey" value="{{.role.RoleKey}}"
                       readonly="true">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">数据范围：</label>
            <div class="col-sm-8">
                <select id="dataScope" name="dataScope" class="form-control m-b">
                    <option value="1" {{if eq .role.DataScope "1"}}selected{{end}}>全部数据权限</option>
                    <option value="2" {{if eq .role.DataScope "2"}}selected{{end}}>自定数据权限</option>
                    <option value="3" {{if eq .role.DataScope "3"}}selected{{end}}>本部门数据权限</option>
                    <option value="4" {{if eq .role.DataScope "4"}}selected{{end}}>本部门及以下数据权限</option>
                    <option value="5" {{if eq .role.DataScope "5"}}selected{{end}}>仅本人数据权限</option>
                </select>
                <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 特殊情况下，设置为“自定数据权限”</span>
            </div>
        </div>
        <div class="form-group" id="authDataScope"
             style="display:{{if eq .role.DataScope "2"}} block{{else}}none {{end}}">
            <label class="col-sm-3 control-label">数据权限</label>
            <div class="col-sm-8">
                <div id="deptTrees" class="ztree"></div>
            </div>
        </div>
    </form>
</div>
{{template "footer" (OssUrl)}}
<script src="{{OssUrl}}/resource/ajax/libs/jquery-ztree/3.5/js/jquery.ztree.all-3.5.js"></script>
<script type="text/javascript">

    $(function () {
        var url = ctx + "/system/dept/roleDeptTreeData?roleId=" + $("#roleId").val();
        var options = {
            id: "deptTrees",
            url: url,
            check: {enable: true, nocheckInherit: true, chkboxType: {"Y": "ps", "N": "ps"}},
            expandLevel: 2
        };
        $.tree.init(options);
    });

    function submitHandler() {
        if ($.validate.form()) {
            edit();
        }
    }

    function edit() {
        var roleId = $("input[name='roleId']").val();
        var roleName = $("input[name='roleName']").val();
        var roleKey = $("input[name='roleKey']").val();
        var dataScope = $("#dataScope").val();
        var deptIds = $.tree.getCheckedNodes();
        $.ajax({
            cache: true,
            type: "POST",
            url: ctx + "/system/role/authDataScope",
            data: {
                "roleId": roleId,
                "roleName": roleName,
                "roleKey": roleKey,
                "dataScope": dataScope,
                "deptIds": deptIds
            },
            async: false,
            error: function (request) {
                $.modal.alertError("系统错误");
            },
            success: function (data) {
                $.operate.successCallback(data);
            }
        });
    }

    $("#dataScope").change(function (event) {
        var dataScope = $(event.target).val();
        dataScopeVisible(dataScope);
    });

    function dataScopeVisible(dataScope) {
        if (dataScope == 2) {
            $("#authDataScope").show();
        } else {
            $._tree.checkAllNodes(false);
            $("#authDataScope").hide();
        }
    }
</script>
</body>
</html>
{{end}}